Page({
    data: {
      username: '',
      password: '',
      confirmPassword: '',
      phone: '',
      address: '',
      isAgreementChecked: true,
      captcha: ''
    },
  
    // 输入处理函数
    onUsernameInput(e) {
      this.setData({ username: e.detail.value });
    },
    onPasswordInput(e) {
      this.setData({ password: e.detail.value });
    },
    onConfirmPasswordInput(e) {
      this.setData({ confirmPassword: e.detail.value });
    },
    onPhoneInput(e) {
      this.setData({ phone: e.detail.value });
    },
    onAddressInput(e) {
      this.setData({ address: e.detail.value });
    },
    onCaptchaInput(e) {
      this.setData({ captcha: e.detail.value });
    },
    onAgreementChange(e) {
      this.setData({ isAgreementChecked: e.detail.value });
    },
  
    // 返回上一页
    onClickLeft() {
      const pages = getCurrentPages();
      if (pages.length > 1) {
        wx.navigateBack({
          delta: 1,
          success: () => console.log('成功返回上一页'),
          fail: (err) => console.error('返回上一页失败:', err)
        });
      } else {
        console.log('当前页面栈中只有一个页面，无法返回上一页');
      }
    },
  
    // 发送验证码
    sendCaptcha() {
      if (!this.data.phone) {
        wx.showToast({ title: '请输入手机号', icon: 'none' });
        return;
      }
      wx.showToast({ title: '验证码已发送', icon: 'success' });
      // 这里可以添加发送验证码的API调用
    },
  
    // 注册提交
    onRegister() {
      const { username, password, confirmPassword, phone, address, isAgreementChecked, captcha } = this.data;
  
      // 验证逻辑
      if (!username || !password || !confirmPassword || !phone || !address || !captcha) {
        wx.showToast({ title: '请填写完整信息', icon: 'none' });
        return;
      }
      if (password !== confirmPassword) {
        wx.showToast({ title: '两次密码不一致', icon: 'none' });
        return;
      }
     
  
      // 注册API调用
      wx.request({
        url: 'http://localhost:8080/pubUser/register',
        method: 'POST',
        data: { username, password, phone, address },
        success: (res) => {
          if (res.data.code === 1) {
            wx.showToast({ title: '注册成功', icon: 'success' });
            setTimeout(() => wx.navigateBack(), 1500);
          } else {
            wx.showToast({ title: res.data.msg || '注册失败', icon: 'none' });
          }
        },
        fail: (err) => {
          console.error('注册请求失败:', err);
          wx.showToast({ title: '网络错误', icon: 'none' });
        }
      });
    }
  });
  